#!/usr/local/bin/python

#  Copyright (c) 2007 Freight Logistics, Inc.
#  Author Ryan Parrish <rparrish@freightlogistics.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 


import os
import sys
import cPickle as pickle

import Pyro.core

from configobj import ConfigObj
import pysvn

config = ConfigObj(os.environ['SVC_CONFIG'])

pyroSolution = Pyro.core.getProxyForURI("PYROLOC://%s:7766/solution" % config['SVC_SERVER'])

#this is the elem that is getting commited
inputFile = os.environ['TM_FILEPATH']
#inputFile = sys.argv[1]

commitMessage = "Server commit to mainline"
#commitMessage = sys.argv[2]

client = pysvn.Client()
changes = client.status(inputFile, update=True)[0]

if changes.repos_text_status == pysvn.wc_status_kind.modified:
    print "This file is out of date"
    sys.exit(-1)
    
folder = '/'.join(inputFile.split('/')[:-1])
method = inputFile.split('/')[-1].split('.')[0]

metaDataFile = open(folder + '/.' + method, 'rb')
metaData = pickle.load(metaDataFile)
metaDataFile.close()

methodFile = open(inputFile, 'r')
propertyValue = methodFile.read()
methodFile.close()

commmitInput = dict(metaData = metaData, propertyValue=propertyValue)
try:
    pyroSolution.commit_method(commmitInput, commitMessage)
    client.update(inputFile)
except Exception,x:
    print ''.join(Pyro.util.getPyroTraceback(x))